package com.zmn.mcc.admin.controller.role.permit;

import com.zmn.common.dictionary.GlobalDict;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.mcc.model.entity.role.McRole;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author heguanghua
 * @since 2020/04/15 10:50
 */
@Controller
@RequestMapping("/role/permit/")
public class RolePermitListController extends RolePermitBaseController {

    /**
     * 模块/权限树状列表
     */
    @RequestMapping("tree")
    public String config(HttpServletRequest request,
                         @RequestParam(value = "roleId", defaultValue = "0") Integer roleId,
                         Model model) {

        McRole obj = NumberUtil.isNotNullOrZero(roleId) ? roleService.findByKey(roleId) : new McRole();

        model.addAttribute("obj", obj);

        // 是否拥有"权限管理员角色"
        model.addAttribute("isAssignAdmin", staffBService.isAssignAdmin(getStaffId()));

        return "role/permit/list_tree";
    }

    @RequestMapping("ajaxTree")
    @ResponseBody
    public ResponseDTO ajaxTree(HttpServletRequest request, HttpServletResponse response,
                                @RequestParam(value = "roleId", defaultValue = "0") Integer roleId) {

        ResponseDTO result = roleBService.listModulePermit(roleId, getStaffId(), GlobalDict.STATUS_ENABLE);
        return result;
    }
}
